;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Barcode.RePrint", [])
        .controller("RePrintCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$timeout", "$filter", "UserContextService", "EventBus", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $timeout, $filter, UserContextService, EventBus) {
            $scope.handleBlur = function(e) {
                $scope.condition.materialNos = UserContextService.MultiInput(e.target.value);
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.toolbarConfig = {
                showPrintConf: false,
                showAdvanceSearch: false,
                showColConfig: false
            };
            $scope.markInfo = {};
            $scope.showDatepickerBegin = false;
            $scope.showDatepickerEnd = false;
            $scope.condition = {
                beginDate: ybWidget.FormatStartDate(+new Date()),
                endDate: ybWidget.FormatEndDate(+new Date())
            };
            $scope.tempSet = {
                qrCodeSize: 85,
            };
            $scope.dataList = [];
            $scope.tableConfig = {
                scrollWidth: 1800,
                fixedWidth: 140,
                tableCol: [
                    {
                        type: "",
                        name: "零件编号",
                        dataField: "materialsNo",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "零件中文名",
                        dataField: "materialsDes",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "原始数量",
                        dataField: "totalQty",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "打印数量",
                        dataField: "currentQty",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "补打次数",
                        dataField: "reprintCount",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "状态",
                        dataField: "statusDesc",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "序号",
                        dataField: "seq",
                        width: 60,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "操作时间",
                        dataField: "createTimeShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "操作人",
                        dataField: "createName",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    /*{
                     type: "",
                     name: "单据类型",
                     dataField: "statusDesc",
                     width: 100,
                     isShow: true,
                     isFixed: false,
                     toSort: false,
                     sortType: ""
                     },*/
                    {
                        type: "",
                        name: "单据号",
                        dataField: "billNo",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "条码",
                        dataField: "barcode",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "条码类型",
                        dataField: "barcodeFlagShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "配送指令号",
                        dataField: "deliveryInsNo",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "VIN码",
                        dataField: "vinid",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "图号",
                        dataField: "orawyd",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "条码描述",
                        dataField: "barcodeDesc",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "规格描述",
                        dataField: "model",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "出库单号",
                        dataField: "businessOrder",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        name: "储位",
                        dataField: "locationCode",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "储位描述",
                        dataField: "locationDesc",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 100,
                        isShow: true,
                        isFixed: true,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };
            var lodop;
            $scope.printInfo = {};

            var btnPermissionCode = {
                rePrintPrint: window.ActionCode.rePrintPrint
            };

            $scope.btnPermission = {
                rePrintPrint: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {scrollLeft: {left: $scope.tableConfig.fixedWidth + "px"}};
            };

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.search = function (curpage) {
                localStorage.setItem($scope.storageKey, JSON.stringify($scope.condition));
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {
                    beginDate: ybWidget.FormatStartDate(+new Date()),
                    endDate: ybWidget.FormatEndDate(+new Date())
                };
            };

            $scope.print = function (item, type) {
                updatePrint(type, [item]);
            };
            
            $scope.batchPrint = function (type) {
                updatePrint(type, $scope.dataList.filter(d => d.isChecked));
            };

            function updatePrint(type, printListTemp) {
                const selBarcodes = printListTemp.map(sd => sd.barcode);
                if (selBarcodes.length == 0) {
                    ybWidget.ShowMessage('请选择要打印的条码');
                    return;
                }

                var service = "device";
                var reqUrl = "/barcode/updatePrintCount";
                var param = {
                    barcodes: selBarcodes
                };
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    startPrint(type, printListTemp)
                });
            }

            $scope.printList = [];
            async function startPrint(type, printListArr) {
                $scope.printList = type === "preview" && printType !== 1 ? printListArr : UserContextService.ListGroup(printListArr, 20);

                var printType = $scope.tempSet.printType;
                lodop = UserContextService.GetLodop();
                if (!lodop) {
                    return;
                }

                if (!await UserContextService.InitPrinter(lodop, true, printType)) {
                    return;
                }
                
                setTimeout(function () {
                    UserContextService.ListPrint($scope.printList, lodop, printType, type === "preview");

                    $scope.printList = [];
                    getDataList();
                }, 300)
            }

            $scope.selectAll = function () {
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    $scope.dataList[i].isChecked = $scope.markInfo.allChecked;
                }
            };

            function checkAll() {
                var selectAll = false;
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    if (!$scope.dataList[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.checkDate = function (item, start, end) {
                if ((angular.isUndefined(item[start]) || !item[start]) && (angular.isUndefined(item[end]) || !item[end])) {
                    return;
                }
                if (!!item[start] && (angular.isUndefined(item[end]) || !item[end])) {
                    item[start] = ybWidget.FormatStartDate(item[start]);
                    return;
                }
                if ((angular.isUndefined(item[start]) || !item[start]) && !!item[end]) {
                    item[end] = ybWidget.FormatEndDate(item[end]);
                    return;
                }
                var tmpTime = item[start];
                if (item[end] < item[start]) {
                    item[start] = ybWidget.FormatStartDate(item[end]);
                    item[end] = ybWidget.FormatEndDate(tmpTime);
                } else {
                    item[start] = ybWidget.FormatStartDate(item[start]);
                    item[end] = ybWidget.FormatEndDate(item[end]);
                }
            };
            $scope.selectSingle = function (item) {
                if (!item.isChecked) {
                    $scope.markInfo.allChecked = false;
                } else {
                    if (!checkAll()) {
                        $scope.markInfo.allChecked = false;
                    } else {
                        $scope.markInfo.allChecked = true;
                    }
                }
            };
            function getDataList(curPage) {
                var service = "device";
                var reqUrl = "/barcode/list";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                param.filters.beginDate = new Date($scope.condition.beginDate).getTime();
                param.filters.endDate = new Date($scope.condition.endDate).getTime();
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                    $scope.markInfo.allChecked = false;
                    angular.forEach($scope.dataList, function (item) {
                        item.isChecked = false;
                        item.barcodeFlagShow = item.barcodeFlag ? '分条码' : '总条码';
                        item.createTimeShow = $filter("YbDate")(item.createTime, "yyyy-MM-dd HH:mm:ss");
                    })
                });
            }

            (function () {
                $scope.storageKey = $state.current.name;
                UserContextService.LocalCache($scope.storageKey, $scope.condition);

                $scope.setFixedWidth();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);